<?php
	session_start();

	//Conexión a la bd
	$link = mysqli_connect('localhost', 'root', '') or die("Failed to connect to MySQL: " . mysql_error());
	if (mysqli_connect_errno()) {
	        echo 'Failed to connect to MySQL';
	        exit;
	}
	$sql = "USE web_db";
	$query = mysqli_query($link,$sql);

	$mec_id = $_POST['mec_id'];
	$pilot_id = $_POST['pilot_id'];

	$sql = "INSERT INTO mecha(mec_id, id) VALUES ('$mec_id', '$pilot_id')";
	if($query = mysqli_query($link,$sql)){
		echo "Mecha insertado en tabla<br>";
	}

	$parts = array(
			$_POST['head'],
			$_POST['chest'],
			$_POST['weapon1'],
			$_POST['weapon2'],
			$_POST['arms'],
			$_POST['legs'],
			$_POST['other']);
	
	foreach($parts as $part){
		$sql = "INSERT INTO mech_part(mec_id, sn) VALUES ('$mec_id','$part')";
		if($query = mysqli_query($link,$sql)){
			echo "Pieza ".$part." insertada<br>";
		}
	}
	$_SESSION['new_mecha']="si";

	header("location:mecha.php");
?>